package com.snap.vseries.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;

import com.snap.vseries.R;
import com.snap.vseries.adapter.DeviceInfoAdapter;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.service.ExpController;
import com.snap.vseries.ui.EggActivity;
import com.snap.vseries.ui.GeneralActivity;
import com.snap.vseries.ui.MyInfoActivity;
import com.snap.vseries.ui.widget.DeviceDetailActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyFragment extends BaseFragment {
    @BindView(R.id.device_rv)
    RecyclerView deviceRv;
    private DeviceInfoAdapter adapter;
    @BindView(R.id.device_fl)
    FrameLayout device_fl;

    @Override
    protected int layoutResId() {
        return R.layout.fragment_my;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();

    }

    private void initViews() {
        List<String> controllerList=new ArrayList<>();
        for (int i =1; i <= 8; i++) {
            controllerList.add(i+"");
        }
        //===========================测试数据==============================
        deviceRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        adapter = new DeviceInfoAdapter(getActivity(), controllerList);
        deviceRv.setAdapter(adapter);
        adapter.setOnItemClickLitener((view, position) -> {
            Intent intent = new Intent(getContext(), DeviceDetailActivity.class);
            intent.putExtra("deviceId", controllerList.get(position));
            getContext().startActivity(intent);
        });
        device_fl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), DeviceDetailActivity.class);
                intent.putExtra("deviceId", "1");
                getContext().startActivity(intent);
            }
        });
    }

    //********************************************5s内连续点10下进彩蛋页面*****************************************
    private long[] counter = new long[10];

    @OnClick(R.id.egg_entrance)
    void startEgg() {
        for (int i = 0; i < 9; i++) {
            counter[i] = counter[i + 1];
        }
        long t = System.currentTimeMillis();
        counter[9] = t;
        if (t - counter[0] < 5000) {
            startActivity(new Intent(getContext(), EggActivity.class));
            Arrays.fill(counter, 0);
        }
    }


    @OnClick({R.id.ll_account, R.id.ll_general})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.ll_account:
                MyInfoActivity.startActivity(getActivity());
                break;
            case R.id.ll_general:
                GeneralActivity.startActivity(getActivity());
                break;
            default:
        }
    }


}
